package com.feature.shop.user.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.feature.core.domain.PageVo;
import com.feature.core.domain.Response;
import com.feature.core.domain.SelectVo;
import com.feature.core.util.AssertUtil;
import com.feature.shop.auth.api.vo.LoginUserVo;
import com.feature.shop.core.util.ShopApplictionContext;
import com.feature.shop.user.api.ShopUserApi;
import com.feature.shop.user.api.request.ShopUserListRequest;
import com.feature.shop.user.api.request.ShopUserPageRequest;
import com.feature.shop.user.api.request.ShopUserRegisterRequest;
import com.feature.shop.user.api.request.ShopUserUpdateRequest;
import com.feature.shop.user.api.vo.ShopRoleVo;
import com.feature.shop.user.api.vo.ShopUserVo;
import com.feature.shop.user.model.entity.ShopRoleEntity;
import com.feature.shop.user.service.ShopRoleService;
import com.feature.shop.user.service.ShopUserRoleService;
import com.feature.shop.user.service.ShopUserService;

import jakarta.validation.Valid;

@RestController
public class ShopUserController implements ShopUserApi {

    @Autowired
    ShopUserService shopUserService;
    @Autowired
    ShopUserRoleService shopUserRoleService;
    @Autowired
    ShopRoleService shopRoleService;

    @Override
    public Response<List<ShopUserVo>> queryUserList(ShopUserListRequest request) {
        List<ShopUserVo> list = shopUserService.findList(request);
        return Response.ok(list);
    }

    @Override
    public Response<PageVo<ShopUserVo>> queryUserPage(ShopUserPageRequest request) {
        PageVo<ShopUserVo> list = shopUserService.findPageList(request);
        return Response.ok(list);
    }

    @Override
    public Response<ShopUserVo> queryUserByName(String userName) {
        ShopUserVo vo = shopUserService.queryUserByName(userName);
        return Response.ok(vo);
    }

    @Override
    public Response<ShopUserVo> queryUserById(Integer userId) {
        ShopUserVo vo = shopUserService.queryUserById(userId);
        return Response.ok(vo);
    }

    @Override
    public Response<?> register(ShopUserRegisterRequest req) {
        LoginUserVo loginUser = ShopApplictionContext.getLoginUser();
        shopUserService.register(req, loginUser);
        return Response.ok();
    }

    @Override
    public Response<List<ShopRoleVo>> queryRoleByUserId(Integer userId) {
        List<ShopRoleVo> list = shopUserRoleService.queryRoleByUserId(userId);
        return Response.ok(list);
    }

    @Override
    public Response<String> uploadFile(MultipartFile file) {
        String url = shopUserService.uploadFile(file);
        return Response.ok(url);
    }

    @Override
    public Response<List<SelectVo>> querySelectRoles() {
        List<ShopRoleEntity> list = shopRoleService.list();
        return Response.ok(list.stream().map(v -> new SelectVo(v.getId(), v.getName())).toList());
    }

    @Override
    public Response<?> updateUser(@Valid ShopUserUpdateRequest req) {
        LoginUserVo loginUser = ShopApplictionContext.getLoginUser();
        shopUserService.updateUser(req, loginUser);
        return Response.ok();
    }

    @Override
    public Response<?> deleteUser(Integer userId) {
        AssertUtil.notNull(userId, "userId不能为空");
        LoginUserVo loginUser = ShopApplictionContext.getLoginUser();
        shopUserService.deleteUser(userId, loginUser);
        return Response.ok();
    }
}
